
package com.xian.gaoxiao.ui.activity;

import com.xian.gaoxiao.MainApplication;
import com.xian.gaoxiao.R;
import com.xian.gaoxiao.ui.widget.ContainerLayout;
import com.xian.gaoxiao.ui.widget.InfoLayout;
import com.xian.gaoxiao.ui.widget.TapLayout;
import com.xian.gaoxiao.utils.TapSizeTools;
import com.xian.gaoxiao.utils.TaskConstants;
import com.xian.gaoxiao.utils.Utilities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainAc extends Activity implements OnClickListener {

    public static final int TAP_COUNT = 4;

    private static final int TAP_MARGIN = 4;
    private static final float TAP_SCALE = 0.85f;

    public static int currentState;

    private int screenHeight;
    private int screenWidth;
    private int titleHeight;
    private int statusBarHeight;

    public static int focusHeight;
    public static int focusWidth;
    public static int unFocusHeight;
    public static int unFocusWidth;
    public static int commonHeight;
    public static int commonWidth;

    private long exitTime;

    private Context mContext;
    private LayoutInflater inflater;

    private TextView titleTV;
    private LinearLayout titleLeft, titleRight;

    private RelativeLayout bodyLayout, titleLayout;
    private View mainView, bodyView;
    private InfoLayout infoLayout;
    private ContainerLayout[] containerLayout = new ContainerLayout[TAP_COUNT];

    private Toast mToast;
    private MainApplication mApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        mContext = this;
        mApp = (MainApplication) this.getApplication();
        initView();
        computeViewSize();
        InitViewData();
        setContentView(mainView);

    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return super.onTouchEvent(event);
    }

    private void initView() {
        inflater = LayoutInflater.from(mContext);
        mainView = inflater.inflate(R.layout.item_mainview, null);
        titleLayout = (RelativeLayout) mainView
                .findViewById(R.id.item_mainview_titlecontainer);
        titleTV = (TextView) mainView.findViewById(R.id.item_mainview_tv_headtitle);
        titleLeft = (LinearLayout) mainView.findViewById(R.id.item_mainview_titleleft);
        titleRight = (LinearLayout) mainView.findViewById(R.id.item_mainview_titleright);

        titleLeft.setOnClickListener(this);
        titleTV.setOnClickListener(this);
        titleRight.setOnClickListener(this);

        bodyView = inflater.inflate(R.layout.ac_affairlist, null);
        bodyLayout = (RelativeLayout) mainView
                .findViewById(R.id.item_mainview_fd_base);
        infoLayout = (InfoLayout) mainView
                .findViewById(R.id.ac_affairlist_infoboard);

        containerLayout[0] = (ContainerLayout) bodyView
                .findViewById(R.id.fd_ll_lefttop_container);
        containerLayout[1] = (ContainerLayout) bodyView
                .findViewById(R.id.fd_ll_righttop_container);
        containerLayout[2] = (ContainerLayout) bodyView
                .findViewById(R.id.fd_ll_leftbottom_container);
        containerLayout[3] = (ContainerLayout) bodyView
                .findViewById(R.id.fd_ll_rightbottom_container);

        containerLayout[0].setPos(0);
        containerLayout[1].setPos(1);
        containerLayout[2].setPos(2);
        containerLayout[3].setPos(3);

        containerLayout[0].setOtherLayouts(containerLayout);
        containerLayout[1].setOtherLayouts(containerLayout);
        containerLayout[2].setOtherLayouts(containerLayout);
        containerLayout[3].setOtherLayouts(containerLayout);

        bodyLayout.addView(bodyView);

    }

    private void computeViewSize() {
        screenHeight = mApp.getScreenHeight();
        screenWidth = mApp.getScreenWidth();
        statusBarHeight = mApp.getStatusBarHeight();
        titleHeight = titleLayout.getLayoutParams().height;

        focusWidth = (int) (screenWidth * TAP_SCALE) - TAP_MARGIN;
        unFocusWidth = unFocusHeight = screenWidth - focusWidth - 2
                * TAP_MARGIN;
        focusHeight = screenHeight - titleHeight - statusBarHeight
                - unFocusHeight - 2 * TAP_MARGIN;

        commonHeight = (screenHeight - titleHeight - statusBarHeight) / 2
                - TAP_MARGIN;
        commonWidth = screenWidth / 2 - TAP_MARGIN;
        Utilities.print(titleHeight + ":" + titleLayout.getMeasuredWidth()
                + ":" + commonHeight + ":" + commonWidth);
    }

    private void InitViewData() {
        currentState = TaskConstants.STATUS_POS_NONE;
        for (ContainerLayout layout : containerLayout) {
            layout.setTapInit();
        }
    }

    @Override
    protected void onDestroy() {

        // TODO Auto-generated method stub
        Utilities.print("main------------ondestroy");
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        Utilities.print(keyCode);
        switch (keyCode) {

            case KeyEvent.KEYCODE_BACK:
                if (currentState != TaskConstants.STATUS_POS_NONE) {
                    InitViewData();
                    return true;
                }

                long curTime = System.currentTimeMillis();
                if ((curTime - exitTime) < TaskConstants.EXIT_TIME_DURATION) {
                    finish();
                    if (mToast != null)
                        mToast.cancel();
                } else {
                    mToast = Toast.makeText(mContext, "再按一次退出修仙日程",
                            TaskConstants.EXIT_TIME_DURATION);
                    mToast.show();
                    exitTime = curTime;
                }
                Utilities.print("back");
                break;
            case KeyEvent.KEYCODE_MENU:
                Utilities.print("menu");
                break;

            default:
                break;
        }
        return true;
    }

    private void gotoAddTask() {
        Intent intent = new Intent(this, TaskItemAddAndEditAc.class);
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.item_mainview_titleright:
                gotoAddTask();
                break;
            case R.id.item_mainview_titleleft:

                break;
            case R.id.item_mainview_tv_headtitle:

                break;

            default:
                break;
        }
    }

}
